<?php
namespace app\admin\controller;

use think\Controller;

class Conf extends Controller
{
    public function conflist()
    {
        $conf = db('conf');
        if (request()->isPost()) {
            $data         = input('post.');
            $check2D      = $conf->field('ename')->where(array('form_type' => 'checkbox'))->select();
            $checkFiedlds = array();
            if ($check2D) {
                foreach ($check2D as $k => $v) {
                    $checkFiedlds[] = $v['ename'];
                }
            }
            $check1D = array();
            foreach ($data as $k => $v) {
                $check1D[] = $k;
                if (is_array($v)) {
                    $v = implode(',', $v);
                    $conf->where(array('ename' => $k))->update(['value' => $v]);
                } else {
                    $conf->where(array('ename' => $k))->update(['value' => $v]);
                }
            }
            // dump($checkFiedlds);die;
            //若果复选框没有一个选中，则设置为空

            foreach ($checkFiedlds as $k => $v) {
                if (!in_array($v, $check1D)) {
                    $conf->where(array('ename' => $v))->update(['value' => '']);
                }
            }

            if ($_FILES) {
                foreach ($_FILES as $k => $v) {
                    if ($v['tmp_name']) {
                        $imgs = $conf->field('value')->where(array('ename' => $k))->find();
                        if ($imgs['value']) {
                            $oimg = IMG_UPLOADS . $imgs['value'];
                            if (file_exists($oimg)) {
                                @unlink($oimg);
                            }
                        }
                        $imgSrc = $this->upload($k);
                        $conf->where(array('ename' => $k))->update(['value' => $imgSrc]);
                    }
                }
            }

            $this->success('修改成功');

        }

        $ShopConfRes  = $conf->where(array('conf_type' => 1))->order('sort desc')->select();
        $GoodsConfRes = $conf->where(array('conf_type' => 2))->order('sort desc')->select();
        $this->assign([
            'ShopConfRes'  => $ShopConfRes,
            'GoodsConfRes' => $GoodsConfRes,

        ]);
        return view();
    }
    public function lst()
    {
        $conf = db('conf');
        if (request()->isPost()) {
            $data = input('post.');
            foreach ($data['sort'] as $k => $v) {
                $data['sort'] = $conf->where(array('id' => $k))->update(['sort' => $v]);
            }
            $this->success('排序成功 ！');

        }
        $confRes = $conf->order('sort desc')->paginate(10);
        $this->assign([
            'confRes' => $confRes,

        ]);
        return view('list');
    }

    public function add()
    {
        if (request()->isPost()) {

            $data = input('post.');

            //如果是中文逗号换成英文逗号
            if ($data['form_type'] == 'radio' || $data['form_type'] == 'checkbox' || $data['form_type'] == 'select') {
                $data['values'] = str_replace('，', ',', $data['values']);
                $data['value']  = str_replace('，', ',', $data['value']);
            }
            //验证
            $validate = validate('Conf');
            if (!$validate->check($data)) {
                $this->error($validate->getError());

            }
            $add = db('conf')->insert($data);

            if ($add) {

                $this->success('添加配置成功 ！', 'lst');
            } else {

                $this->error('添加配置失败');
            }
            return;
        }

        return view();
    }
    public function edit()
    {

        if (request()->isPost()) {
            $data = input('post.');

            //如果是中文逗号换成英文逗号
            if ($data['form_type'] == 'radio' || $data['form_type'] == 'checkbox' || $data['form_type'] == 'select') {
                $data['values'] = str_replace('，', ',', $data['values']);
                $data['value']  = str_replace('，', ',', $data['value']);
            }
            //验证
            $validate = validate('Conf');
            if (!$validate->check($data)) {
                $this->error($validate->getError());

            }
            $save = db('conf')->update($data);
            if ($save !== false) {

                $this->success('修改配置成功 ！', 'lst');
            } else {

                $this->error('修改配置失败');
            }
            return;
        }

        $id = input('id');

        $conf = db('conf')->find($id);

        $this->assign([
            'conf' => $conf,

        ]);

        return view();
    }
    public function del($id)
    {
        $del = db('conf')->delete($id);

        if ($del) {

            $this->success('删除配置成功 ！', 'lst');
        } else {

            $this->error('删除配置失败');
        }
    }
    public function upload($imgName)
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file($imgName);

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
            if ($info) {

                return $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
                die;
            }
        }
    }
}
